/******************************************************************************
* decompose place vs person components at cz level 
* using a two-way fixed effect model
* also run the model across bootstrap samples
*****************************************************************************/



*regression specification:
global runReg " i.rel_q_prime i.asofy i.asofm i.age  [aw=numobs] "
	

if $fe_cz_boot_id==1{

	*** bootstrap samples
	use num_mv varID numobs asofdate move_t rel_q* cz ${Geo} using "${dataRoot}/tu_mover_delta${ext}.dta", clear
		
	cleanSample2WayFE

	isid asofdate varID

	sum asofdate
	keep if asofdate==`r(min)'

	isid varID

	*check total number of valid movers
	sum numobs
	expand numobs
	gen numobs_sample=1
	keep numobs_sample varID



	*** bootstrap at individual level
	set seed 20200101
	forval i=1/$nBS{
		preserve
		
		bsample
		
		collapse (sum) numobs_sample, by(varID)
		
		sum numobs_sample
		compress
		save "${dataRoot}/bootstrap_sample`i'.dta", replace
		
		restore

	}

}

*****************************************************************************
** place vs person cz level decomposition
*****************************************************************************
if $fe_cz_estimate==1{


	*load cut version to get varlist
	use "${dataRoot}/tu_mover_delta${ext}_cut.dta", clear


	*mover data (load one variable at a time)
	foreach var of varlist $keyMvVars{
		use num_mv - numobs `var' avg_`var' delta_`var' asofy - rank rel_q - max_rel_q cz ${Geo} using "${dataRoot}/tu_mover_delta${ext}.dta", clear
		
		cleanSample2WayFE
		
		labelVars 0 0
		
		**********************************************************
		// run regression to estimate place fixed effect at cz level
		// first using the sample and then bootstrap
		**********************************************************
		*** for each vars, run two regression, one with variation FE, one without
		
		
		forval t=0/$nBS{
			forval i=1/2{
				preserve
				
					if `t'>0{
						di "sample `t' reg `i'/2"
						merge m:1 varID using "${dataRoot}/bootstrap_sample`t'.dta"
						qui drop numobs
						qui rename numobs_sample numobs
						qui keep if _merge==3
					}				
					
					sum `var'
					
					*rescale non-movers, so the overal weight of movers vs non-mvoers are the same
					wtdAdj numobs
					
					*FGW equation(2)
					if `i'==1{
						qui areg `var' i.cz ${runReg}, absorb(varID)
					}
					else{
						qui reg  `var' i.cz ${runReg}	
					}
					 
					*Get coefficients on place FE
					parmest, norestore 
					
					qui gen cz = regexs(1) if regexm(parm, "(^[0-9]+)b?\.cz")
					qui keep if cz != "" 
					destring cz, replace
					
					qui gen beta = estimate 
					qui gen se = stderr
					qui gen upper = beta + 1.96*stderr
					qui gen lower = beta - 1.96*stderr
		
					* save place FE
					drop estimate stderr dof t p min95 max95
					compress
					sum cz
					if `i'==1{
						save "${dataRoot}/TU_place_estimates_`var'_sample`t'.dta", replace
					}
					else{
						save "${dataRoot}/TU_place_avg_`var'_sample`t'.dta", replace		
					}			
					
				restore	
			}
		}
	}

}



*****************************************************************************
** aggregate results
*****************************************************************************
if $fe_cz_merge_est==1{	
	*** collapse data to cz level and merge with estimated place FE
	use cz asofdate numobs $keyMvVars move_t num_mv rel_q* varID using "${dataRoot}/tu_mover_delta${ext}.dta", clear

	cleanSample2WayFE


	sum asofdate
	tab num_mv if asofdate==`r(min)'

	expand numobs

	sum asofdate
	tab num_mv if asofdate==`r(min)'


	wtdAdj numobs




	*wtd average foreach cz X year-quarter
	foreach var in $keyMvVars{
		qui replace `var' = `var'*numobs

	}
	collapse  (sum) $keyMvVars numobs, by (cz asofdate) fast

	foreach var in $keyMvVars{
		qui replace `var' = `var'/numobs
	}

	*simple average over time
	collapse  $keyMvVars numobs, by (cz) fast

	compress
	save "${dataRoot}/place_v_cuctomer_base.dta", replace


	***collapse bootstrapped data
	forval t=1/$nBS{
		preserve
		
		use cz asofdate varID num_mv $keyMvVars using "${hugeDataRoot}/tu_mover_delta${ext}.dta", clear
		merge m:1 varID using "${dataRoot}/bootstrap_sample`t'.dta"
		qui keep if _merge==3
		drop _merge

		*adjust mover vs nonmover weight
		wtdAdj numobs_sample
		collapse $keyMvVars [w=numobs_sample], by (cz asofdate) fast
		collapse $keyMvVars, by (cz) fast
		
		foreach var in $keyMvVars{
			rename `var' `var'_sample`t'
		}
		compress
		save "${dataRoot}/place_v_cuctomer_base_`t'.dta", replace

		restore
		
		
		*merge
		merge 1:1 cz using "${dataRoot}/place_v_cuctomer_base_`t'.dta"
		drop _merge
	}



	** now look up FE estimates
	*merge estimated place FE
	foreach var of varlist $keyMvVars{
		forval t=0/$nBS{
			*place effect
			merge 1:1 cz using "${dataRoot}/TU_place_estimates_`var'_sample`t'.dta" 
			qui tab _merge
			qui keep if _merge==3
			drop _merge
			rename beta `var'_p_`t'
			
			*raw place average conditional on time varying parameters
			merge 1:1 cz using "${dataRoot}/TU_place_avg_`var'_sample`t'.dta" 
			qui tab _merge
			qui keep if _merge==3
			drop _merge
			rename beta `var'_fe_`t'

		}
	}


	*save place vs customer decomposition data
	compress
	save "$dataRoot/TU_place_vs_customer_cz${ext}_v1_bs_wtd.dta", replace
}


*time stamp
display "DONE!! $S_TIME  $S_DATE"

